package cn.itsource.pethome.system.employee.controller;


import cn.itsource.pethome.system.employee.domain.Employee;
import cn.itsource.pethome.system.employee.query.EmployeeQuery;
import cn.itsource.pethome.system.employee.service.IEmployeeService;
import cn.itsource.pethome.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:R
 * @Date:2023/1/2 21:52
 * Description
 */
@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/list")
    @ApiOperation(value = "获取全部员工信息")
    public List<Employee> findAll() {
        return employeeService.findAllBo();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询一条员工信息")
    public Employee findOne(@PathVariable("id") Long id){
        return employeeService.findOne(id);
    }

    @ApiOperation(value = "分页查询" )//swagger的注释
    @PostMapping("/pageList")
    public AjaxResult findQuery(@RequestBody EmployeeQuery query){
        try {
            List<Employee> query1 = employeeService.findQuery(query);
            Long aLong = employeeService.countPage(query);
            return AjaxResult.success().setData(query1).setTotal(aLong);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }

    @PutMapping
    @ApiOperation(value = "保存员工信息【有id则为修改】")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if (employee.getId() == null){
                employeeService.add(employee);
                return AjaxResult.success().setMsg("添加成功");
            }else {
                employeeService.update(employee);
                return AjaxResult.success().setMsg("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除员工")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  AjaxResult.error().setMsg("删除失败");
    }

    @PatchMapping
    @ApiOperation(value = "根据id批量删除员工")
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("批量删除失败");
    }
}
